package ct.zxd.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import ct.zxd.bean.*;
import ct.zxd.db.*;

public class GetShopDish extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		response.setCharacterEncoding("UTF-8");
        response.setContentType("text/xml;character=utf-8");  
        response.setHeader("Cache-Control", "no-cache");
        
        String whichShop=request.getParameter("shopid");
        whichShop = java.net.URLDecoder.decode(whichShop, "utf-8");
		
        List<Map<String,Object>> listSh = null;
        List<Map<String,Object>> listCa = null;
        List<Map<String,Object>> listDi = null;
		        
        try {  
        	
        	Dao dao = new Dao();
			
			listSh = dao.getTableByid("shop",whichShop,null);
			listCa = dao.getTableByid("category",whichShop,null);
			listDi = dao.getTableByid("dish",whichShop,"");
			//小程序读取时要order by categoryid,网站读取order by show_id,用最后一个参数区分""
			
//			String shopName;
//			String[] shopImages;
//			Map<String,Object> mapShop = listSh.get(0);
//			if(mapShop == null){
//				throw new Exception("找不到当前店铺");
//			}else{
//				shopName = (String) mapShop.get("name");
//				String imageString = (String) mapShop.get("images");
//				shopImages = imageString.split("\\|");
//			}
			
        	List<Category> listCategory = new ArrayList<Category>(); 
        	for(Map<String,Object> map : listCa){
        		Category cate = new Category();
        		cate.setName((String)map.get("name"));
        		
        		//小程序端要使用category_id来识别当前种类是否被选中
        		//小程序端category_id是int类型，因为要匹配点击事件的index
        		String cateid = (String)map.get("category_id");
        		cate.setCategory_id(Integer.parseInt(cateid));
        		//cate.setShop_id(Integer.parseInt(whichShop));
        		listCategory.add(cate);
        	}
            
            List listDish = new ArrayList();
            List<Dish> dishes = new ArrayList<Dish>();
            String cateFlag = null;
            for(Map<String,Object> map : listDi){
            	Dish dish = new Dish();
            	
            	//excel函数：(String)map.get("introduce")  ="("&C1&")map.get(#"&D1&"#)"
            	dish.setDish_id((Integer)map.get("id"));
            	dish.setName((String)map.get("name"));
            	dish.setImageURL((String)map.get("imageURL"));
            	dish.setPrice((Float)map.get("price"));
            	dish.setQuantity(0);
            	dish.setMinusStatuses("disabled");	
            	dish.setStockNumber(Integer.parseInt(map.get("stockNumber").toString()));
            	
            	/**下一个dish的category与上一个相比较，
            	 * 如果不相同，说明是另一个类别的dish，需要新建dishes数组
            	 */
            	String category = (String)map.get("category_id");
            	dish.setCategory_id(category);
            	if(cateFlag==null){
            		cateFlag = category;
            		dishes.add(dish);
            		continue;
            	}
            	if(!cateFlag.equals(category)){  //不同种类要新建一个数组
            			listDish.add(dishes);
            			cateFlag = category;
            			dishes = new ArrayList<Dish>();
            	}
            	dishes.add(dish);
            		
            }
            listDish.add(dishes);    //最后一个dishes数组要加入
            
            try {  
            	//创建JsonArray对象，JSONArray则是[]包裹起来的一个数组(Array)，此处调用方法将对象集合装入  
                JSONArray jsonCategory = JSONArray.fromObject(listCategory);  
                JSONArray jsonDish = JSONArray.fromObject(listDish);
//                JSONArray jsonImages = JSONArray.fromObject(shopImages);
               
              //JSONObject是一个{}包裹起来的一个对象(Object)，此处希望以对象为单位进行操作，所以创建JSONObject对象  
                JSONObject jb = new JSONObject();    
                jb.put("category", jsonCategory);
                jb.put("dish", jsonDish);
//                jb.put("images", jsonImages);
//                jb.put("shopname", shopName);
                
                //输出Json数据
                response.getWriter().write(jb.toString());  
        
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
